package com.tjsinfo.tjsoa.mail.action;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.tjsinfo.tjsoa.common.DealString;
import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.mail.bo.ITjsemailReceivedBo;
import com.tjsinfo.tjsoa.mail.bo.ITjsmailBo;
import com.tjsinfo.tjsoa.mail.bo.TjsDraftBo;
import com.tjsinfo.tjsoa.mail.dao.TjsDrefAnnexDao;

import com.tjsinfo.tjsoa.mail.vo.TjsAnnex;
import com.tjsinfo.tjsoa.mail.vo.TjsDraft;
import com.tjsinfo.tjsoa.mail.vo.TjsDraftAnnex;
import com.tjsinfo.tjsoa.message.bo.ITjsMessageBo;
import com.tjsinfo.tjsoa.message.vo.TjsMessage;
import com.tjsinfo.tjsoa.system.vo.Files;
import com.tjsinfo.tjsoa.system.vo.User;

/**
 * 草稿发送
 * @author wangxiaowei
 *
 */
public class DrafSendAction extends Action{
	private TjsDraftBo tjsDraftBo;
	private  ITjsmailBo tjsmailBo;
	private TjsDrefAnnexDao tjsDrefAnnexDao;
	private  ITjsemailReceivedBo tjsemailReceivedBo;
	public ITjsmailBo getTjsmailBo() {
		return tjsmailBo;
	}
	public void setTjsmailBo(ITjsmailBo tjsmailBo) {
		this.tjsmailBo = tjsmailBo;
	}
	public ITjsemailReceivedBo getTjsemailReceivedBo() {
		return tjsemailReceivedBo;
	}
	public void setTjsemailReceivedBo(ITjsemailReceivedBo tjsemailReceivedBo) {
		this.tjsemailReceivedBo = tjsemailReceivedBo;
	}
	public TjsDraftBo getTjsDraftBo() {
		return tjsDraftBo;
	}

	public void setTjsDraftBo(TjsDraftBo tjsDraftBo) {
		this.tjsDraftBo = tjsDraftBo;
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		try {
			HttpSession session=request.getSession();
			User user = (User)session.getAttribute("user");
			
			MessageResources message=getResources(request);
			String deletefujian = request.getParameter("deletefujian");
			String[] deletefujianArray = deletefujian.split(",");
			for(String fujianid : deletefujianArray) {
				if(fujianid!=null &&!"".equals(fujianid)){
					tjsDrefAnnexDao.deleteDrefANNEX(fujianid);
				}
			}
			//提交上传附件
			String fromFilePath=message.getMessage(request.getLocale(), "system.uploadFile.tempFilePath");
			String toFilePath = ToolKit.getProperties("com/tjsinfo/tjsoa/mail/properties/systemResource.properties", this).getProperty("email.fileurl");			
			List<Files> fileList=DealString.fileCopyTofile(request, fromFilePath, toFilePath);
			List<TjsAnnex> list = new ArrayList<TjsAnnex>();
			for (Files files : fileList) {
				TjsAnnex tjsAnnex = new TjsAnnex();
				tjsAnnex.setAnnexAddress(files.getToFilePath());
				tjsAnnex.setAnnexName(files.getNewFileName());
				tjsAnnex.setAnnexZhenName(files.getOldFileName());
				File f = new File(toFilePath+files.getNewFileName()); 
				int size = ((int)(100*f.length())/1024);
				tjsAnnex.setAnnexSize(size);
				list.add(tjsAnnex);
			}			
			String zhusong = request.getParameter("sendid");
			String sendInput = request.getParameter("jubaorenInput");
			if (sendInput!=null && !",".equals(sendInput)) {
				zhusong += sendInput;
			}
			HashMap<String, String> map = new HashMap<String, String>();
			
			String chaosong = request.getParameter("chaosongid");
			String value = request.getParameter("contest");
			String title = request.getParameter("title");
			//TjsAnnex[] tjsAnnexs = new TjsAnnex[3];
			String drafid = request.getParameter("drafid");
		
			//String mailid =	tjsmailBo.addSendMail(title, value, new Date(), 1, SessionUser.getUserId(request), 1,list);//发信
			String[] zhusongarray =  zhusong.split(",");
			String[] chaosongarray =  chaosong.split(",");
			for (int i = 0; i < zhusongarray.length; i++) {
				if(zhusongarray[i]!= null && !"".equals(zhusongarray[i]))
				{
					if (map.get(zhusongarray[i])==null) {
						map.put(zhusongarray[i], "yes");
					}else {
						continue;
					}
					//tjsemailReceivedBo.addTjsmailReceived(mailid, Integer.parseInt(zhusongarray[i]), 1, 1);	
					
					TjsMessage message1 = new TjsMessage();
					message1.setRecuserid(Integer.parseInt(zhusongarray[i]));
					message1.setValue(user.getUsername()+"向您发送邮件："+title+"，请登录TJSOA进行处理。");
					//tjsMessageBo.sendNewMessage(message1);
				}
			}
			for (int i = 0; i < chaosongarray.length; i++) {
				if (chaosongarray[i] !=null &&!"".equals(chaosongarray[i])) {
					//tjsemailReceivedBo.addTjsmailReceived(mailid, Integer.parseInt(chaosongarray[i]), 2, 1);
					
					TjsMessage message1 = new TjsMessage();
					message1.setRecuserid(Integer.parseInt(chaosongarray[i]));
					message1.setValue(user.getUsername()+"向您发送邮件："+title+"，请登录TJSOA进行处理。");
					//tjsMessageBo.sendNewMessage(message1);
				}
			}
			
			tjsDraftBo.deleteTjsDraf(drafid);
			request.setAttribute("linkurl", "tjsinfo/tjsoa/mail/emailDraftList.do");
			request.setAttribute("message", "email.message.sendemail");
			request.setAttribute("count", tjsemailReceivedBo.count(String.valueOf(SessionUser.getUserId(request)),1));
			return mapping.findForward("success");
			
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return mapping.findForward("error");
		}
	}
	public TjsDrefAnnexDao getTjsDrefAnnexDao() {
		return tjsDrefAnnexDao;
	}
	public void setTjsDrefAnnexDao(TjsDrefAnnexDao tjsDrefAnnexDao) {
		this.tjsDrefAnnexDao = tjsDrefAnnexDao;
	}
	
}
